<template>
  <div>
    <van-nav-bar title="购物车" left-text="返回" left-arrow>
      <template #right>
        <van-icon
          name="search"
          size="18"
          @click="$router.push('/SearchGoods')"
        />
      </template>
    </van-nav-bar>
    <div class="catConten">
      <van-empty
        v-if="$store.state.emailLogin == null"
        class="custom-image"
        image="https://sss-1306551685.cos.ap-guangzhou.myqcloud.com/MobileShop/cat.png"
        description="登录可同步购物车商品"
      />
      <van-empty
        v-if="isShow"
        class="custom-image"
        image="https://sss-1306551685.cos.ap-guangzhou.myqcloud.com/MobileShop/cat.png"
        description="您的购物车还没有一件商品哦"
      />
      <!-- 购物车商品集合 -->
      <div v-else class="gwc_list">
        <van-swipe-cell v-for="item in catList" :key="item.CatID">
          <van-cell>
            <template #title
              ><van-tag color="#7232dd">精选商品</van-tag></template
            >
            <template #right-icon>
              <van-tag color="#ffe1e1" text-color="#ad0000">优惠卷</van-tag>
            </template>
          </van-cell>
          <div class="gwc_item">
            <div class="item_1">
              <van-checkbox
                @change="statechange(item.CatID, item.goods_state)"
                v-model="item.goods_state"
                checked-color="#ee0a24"
                >复选框</van-checkbox
              >
            </div>
            <div class="item_2">
              <router-link
                :to="{ name: 'details', params: { gid: item.GoodsID } }"
              >
                <img :src="item.GoodsPic" alt="" />
              </router-link>
            </div>
            <div class="item_3">
              <div class="item_3_1">
                <p class="p_title">{{ item.GoodName }}</p>
                <p class="p_price"></p>
              </div>
              <div class="item_3_2">
                <p class="p_size">
                  颜色:{{ item.Color }} 尺码：{{ item.Size }}
                </p>
                <p class="p_oprice">￥{{ item.GoodsPrice }}</p>
              </div>
              <div class="item_3_3">
                <van-stepper
                  v-model="item.goodsNum"
                  theme="round"
                  button-size="22"
                  disable-input
                  @plus="addnum(item.goodsNum)"
                  @minus="lessnum(item.goodsNum)"
                />
              </div>
            </div>
          </div>
          <template #right>
            <van-button
              square
              text="删除"
              type="danger"
              @click="remove(item.CatID)"
              class="delete-button"
            />
          </template>
        </van-swipe-cell>
      </div>
      <!-- 结算 -->
      <div class="Settlement_box" v-if="$store.state.emailLogin !== null">
        <van-submit-bar
          :price="amount * 100"
          button-text="提交订单"
          @submit="onSubmit"
        >
          <van-checkbox @change="allstate" v-model="allchecked"
            >全选</van-checkbox
          >
        </van-submit-bar>
      </div>
    </div>
    <div class="fbox"></div>
  </div>
</template>
<script>
import { mapState } from "vuex";

export default {
  data() {
    return {
      num: 0,
      time: 0,
      checked: false,
      allchecked: false,
      catList: [],
      isShow: false,
      cid: 0,
    };
  },
  mounted() {},
  created() {
    this.getCatList();
  },
  activated() {
    this.getCatList();
  },
  beforeDestroy() {},

  methods: {
    async onSubmit() {
      const t = this.catList.filter((item) => item.goods_state == true);
      if (t.length === 0) {
        this.$toast("请选中一件商品！");
      } else {
        this.$store.commit("setCheckoutList", t);
        let tt = [];
        this.catList
          .filter((x) => x.goods_state == true)
          .forEach((x) => {
            tt.push(x.CatID);
          });

        const { data: res } = await this.$http.get("delList", {
          params: { CatID: tt },
        });
        console.log(res);
        if (res.status == 200) {
          this.getCatList();
        }
        this.$store.commit("setTotol", this.amount);
        this.$router.push("/checkout");
      }
    },
    async getCatList() {
      try {
        const { data: res } = await this.$http.get("shopCatList", {
          params: { UserID: this.emailLogin[0].userID },
        });

        res.data.forEach((item) => {
          item.goods_state = item.goods_state == 0 ? false : true;
        });

        this.catList = res.data;

        this.$store.commit("setCatList", res.data);
      } catch (error) {}
    },
    statechange(id, state) {
      this.cid = id;
      console.log(state);
      const findresult = this.catList.find((x) => x.CatID === id);
      if (findresult) {
        findresult.goods_state = state;
      }
    },
    allstate(e) {
      this.catList.forEach((x) => (x.goods_state = e));
    },
    addnum(nums) {
      this.$toast.loading({ forbidClick: true });

      clearTimeout(this.time);
      this.time = setTimeout(() => {
        this.$toast.clear();
        // 注意此时修改 value 后会再次触发 change 事件
        nums++;
      }, 300);
    },
    lessnum(num) {
      this.$toast.loading({ forbidClick: true });

      clearTimeout(this.time);
      this.time = setTimeout(() => {
        this.$toast.clear();
        // 注意此时修改 value 后会再次触发 change 事件
        num--;
      }, 300);
    },
    remove(id) {
      this.cid = id;
      this.$dialog
        .confirm({
          message: "确认删除购物车该商品吗？",
          theme: "round-button",
        })
        .then(() => {
          this.beforeClose();
        })
        .catch(() => {
          // on cancel
        });
    },
    beforeClose() {
      this.$toast.loading({ forbidClick: true });
      setTimeout(() => {
        this.$toast.clear();
        this.removeinfo();
      }, 1200);
    },
    async removeinfo() {
      const { data: res } = await this.$http.get("delGoods", {
        params: { CatID: this.cid },
      });
      if (res.status == 200) {
        this.getCatList();
        this.$toast.success("删除成功！");
      }
    },
  },
  computed: {
    ...mapState(["emailLogin", "CatList"]),
    amount() {
      let a = 0;
      this.catList
        .filter((x) => x.goods_state)
        .forEach((x) => {
          a += x.GoodsPrice * x.goodsNum;
        });
      return a;
    },
  },
  watch: {
    catList() {
      if (this.$store.state.emailLogin !== null && this.catList.length === 0) {
        this.isShow = true;
      } else {
        this.isShow = false;
      }
    },
  },
};
</script>
<style lang="less" scoped>
.van-empty {
  padding: 120px 0;
}
.catConten {
}
.goods-card {
  margin: 0;
  background-color: white;
}

.delete-button {
  height: 100%;
}
.van-card__title {
  font-size: 15px;
}
.gwc_list {
  //   padding: 0 0.5rem;
  .gwc_item {
    border-bottom: 1px solid #e3e3e3;
    padding: 0.5rem 0.3rem;
    height: 6.5rem;
    overflow: hidden;
    background-color: white;
    .item_1 {
      float: left;
      width: 1.3rem;
      height: 1.3rem;
      padding-right: 0.7rem;
      margin-top: 1.2rem;
    }
    .item_2 {
      float: left;
      width: 6.5rem;
      height: 6.5rem;
      overflow: hidden;
      padding-right: 0.5rem;
    }
    .item_3 {
      flex: 1;
      height: 4.5rem;
      .item_3_1 {
        height: 3rem;
        overflow: hidden;
        .p_title {
          display: -webkit-box;
          overflow: hidden;
          text-overflow: ellipsis;
          -webkit-line-clamp: 2;
          -webkit-box-orient: vertical;
        }
        .p_price {
          float: right;
          font-size: 0.8rem;
          color: #999;
          text-decoration: line-through;
          margin-right: 0.3rem;
        }
      }
      .item_3_2 {
        // height: 1.05rem;
        // padding-top: 0.2rem;

        .p_size {
          float: left;
          color: #999;
          font-size: 0.8rem;
          width: 60%;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
        }
        .p_oprice {
          float: right;
          color: #fc3535;
          font-size: 1rem;
          margin-right: 0.3rem;
        }
      }
      .item_3_3 {
        height: 1.5rem;
        overflow: hidden;
        padding-top: 0.7rem;
      }
    }
  }
}
.Settlement_box {
  width: 100%;
  max-width: 640px;
  margin: 0 auto;
  background: #fff;

  .van-submit-bar {
    bottom: 57px;
    left: 0;
    z-index: 9999;
    border: 1px solid #f4f4f4;
  }
}
.van-swipe-cell {
  margin-bottom: 10px;
}
</style>